package com.chatcompetition.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chatcompetition.context.BaseContext;
import com.chatcompetition.domain.entity.CompetitionActivity;

import com.chatcompetition.mapper.CompetitionActivityMapper;
import com.chatcompetition.service.CompetitionActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CompetitionActivityServiceImpl extends ServiceImpl<CompetitionActivityMapper, CompetitionActivity> implements CompetitionActivityService {

    @Autowired
    private CompetitionActivityMapper competitionActivityMapper;

    @Override
    public CompetitionActivity getById(Long id) {
        return getById(id);
    }



    @Override
    public boolean update(CompetitionActivity activity) {
        return updateById(activity);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }



    @Override
    public List<CompetitionActivity> listAllCompetitionsAddTeachers() {
        String employeeNumber = BaseContext.getCurrentId();
        // 查询类别为 "竞赛" 的记录并且是自己添加的
        QueryWrapper<CompetitionActivity> wrapper = new QueryWrapper<>();
        wrapper.eq("category", "竞赛")
               .eq("employee_number", employeeNumber);
        return competitionActivityMapper.selectList(wrapper);

    }

    @Override
    public List<CompetitionActivity> getAllCompetitions() {
        return competitionActivityMapper.selectList(null);
    }



    @Override
    public List<CompetitionActivity> listAllActivitiesAddTeachers() {

        String employeeNumber = BaseContext.getCurrentId();
        // 查询类别为 "活动" 的记录
        QueryWrapper<CompetitionActivity> wrapper = new QueryWrapper<>();
        wrapper.eq("category", "活动")
                .eq("employee_number", employeeNumber);
        return competitionActivityMapper.selectList(wrapper);
    }

    @Override
    public List<String> getSubCategoriesByCategory(String category) {
        // 创建查询条件，查询类别为指定类别的记录
        QueryWrapper<CompetitionActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", category);

        // 查询所有符合条件的记录
        List<CompetitionActivity> competitionActivities = competitionActivityMapper.selectList(queryWrapper);

        // 提取所有下分类别，并去重
        List<String> subCategories = competitionActivities.stream()
                .map(CompetitionActivity::getSubCategory)
                .distinct()
                .collect(Collectors.toList());

        return subCategories;
    }

    @Override
    public List<CompetitionActivity> getCompetitionsByFilters(String category, String subCategory) {
        QueryWrapper<CompetitionActivity> queryWrapper = new QueryWrapper<>();
        if (category != null) {
            queryWrapper.eq("category", category);
        }
        if (subCategory != null) {
            log.info("subCategory: " + subCategory);
            queryWrapper.eq("sub_category", subCategory);
        }
        // 打印生成的 SQL 语句，可视化的查看

        System.out.println("Generated SQL: " + queryWrapper.getSqlSegment());
        return competitionActivityMapper.selectList(queryWrapper);
    }
}